package com.atguigu.bigdata.chapter11.window;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @Author lzc
 * @Date 2022/9/9 14:09
 */
public class Flink05_TVF_3 {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        
        
        tEnv.executeSql("create table sensor(" +
                            "   id string, " +
                            "   ts bigint, " +
                            "   vc int, " +
                            "   et as TO_TIMESTAMP_LTZ(ts, 3),  " +// 添加一个时间戳字段, 能否直接认为他就是事件时间?  必能, 必须添加水印
                            "   watermark for et as et - interval '3' second " + // 添加水印
                            ")with(" +
                            "   'connector' = 'filesystem', " +
                            "   'path' = 'input/sensor.txt', " +
                            "   'format' = 'csv' " +
                            ")");
        
        // select .. from table(...) group by id, window_start, window_end
        // select .. from table(...) group window_start, window_end
        /*tEnv
            .sqlQuery("select  " +
                          " id, window_start, window_end, " +
                          " sum(vc) vc_sum " +
                          "from table( tumble( table sensor, descriptor(et), interval '5' second ))" +
                          "group by id, window_start, window_end " +
                          "union " +
                          "select  " +
                          " '',  window_start, window_end, " +
                          " sum(vc) vc_sum " +
                          "from table( tumble( table sensor, descriptor(et), interval '5' second ))" +
                          "group by window_start, window_end ")
            .execute()
            .print();*/
        
        // 分组集用来替换前面的写法
        tEnv
            .sqlQuery("select  " +
                          " id, window_start, window_end, " +
                          " sum(vc) vc_sum " +
                          "from table( tumble( table sensor, descriptor(et), interval '5' second ))" +
                            // window_start, window_end, 这个两个必须执行提供一个
//                          "group by window_start, window_end, grouping sets( (id), () ) "
//                          "group by window_start, window_end, rollup(id) "
                          "group by window_start, window_end, cube(id) "
                          )
            .execute()
            .print();
    }
}
/*
 group by window_start, window_end, grouping sets( (a, b, c), (a,b), (a), () )  // roll up 上钻
 等价于:
    group by window_start, window_end,rollup(a,b,c)
    
  
  多维立方体: 维度的各种组合全部统计一遍
   group by window_start, window_end, grouping sets(
    (a, b, c),
    (a,b), (a, c), (b, c) ,
    (a), (b), (c),
    ()
   )
   
   等价于:
    group by window_start, window_end,cube(a,b,c)
 
 
 */
